#include "main.h"
#include "mainwnd.h"

#include <QCefContext.h>
#include <QCefSetting.h>

int main(int argc, char *argv[])
{
    //QT
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc, argv);

    //CEF
    QCefContext * ctx = new QCefContext();
    Q_ASSERT(ctx != nullptr);

    ctx->getCefSetting().setSubprocessPath((QApplication::applicationDirPath() + "/render.exe"));

#if (defined(DEBUG) || defined(_DEBUG) || !defined(NDEBUG))
    //为方便调试，Debug版本采用分离的渲染进程。Release版本采用自身作为渲染进程
    //ctx->getCefSetting().setSubprocessPath(pathJoin(QApplication::applicationDirPath(), "render.exe"));
#endif

#ifdef QT_CEF_SINGLE_PROCESS
    //单一进程模式
    //ctx->getCefConfig().addCommandLineSwitch("--single-process");
#endif // QT_CEF_SINGLE_PROCESS

    //关闭同源策略
    ctx->getCefConfig().addCommandLineSwitch("--disable-web-security");

    //打开自动播放
    ctx->getCefConfig().addCommandLineSwitchWithValue("autoplay-policy", "no-user-gesture-required");

    //
    QString strCachePath = (QApplication::applicationDirPath() + "/cache");
    QDir dir(strCachePath);
    dir.mkpath(strCachePath);
    ctx->getCefSetting().setCachePath(strCachePath);

    //
    if (ctx->init() > 0)
    {
        //渲染进程
        return 1;
    }

    //主窗口
    MainWnd * wnd = new MainWnd("https://www.baidu.com");
    wnd->show();

    // 消息循环
    int ret = app.exec();

    //wnd
    if (wnd != nullptr)
    {
        delete wnd;
        wnd = nullptr;
    }

    //CEF
    if (ctx != nullptr)
    {
        delete ctx;
        ctx = nullptr;
    }

    return ret;
}
